<?php
namespace Acme\HelloBundle\Controller;


use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class HelloController extends Controller 
{
	//public function indexAction($first_name, $color) # we can write like this when omitting parameters
	//public function indexAction($first_name, $last_name, $color, $foo = 'bar')
	
	//public function indexAction($name)
	public function indexAction($first_name, $last_name)
	{
		// ======== create example with respone object =========
		//return new Response('<html><body>123 '.$name.'</body></html>');
		
		// ======== create example with template =========		
		//return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
		//return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $first_name . ' ' . $last_name));
		
		// ======== redirect to another page =========		
		//return $this->redirect($this->generateUrl('_welcome'), 301);
		//return new RedirectResponse($this->generateUrl('_welcome'));
		
		// ======== forward to other page ==========
		/* $response = $this->forward('HelloBundle:Hello:fancy', array(
		 		'name' => $name,
				'color' => 'green'
		)); */
		/* $httpKernel = $this->container->get('http_kernel');
		$httpKernel->forward('AcmeHelloBundle:Hello:fancy', array(
				'name' => $name,
				'color' => 'green',
			)
		); */
		/* $httpKernel = $this->container->get('http_kernel');
		 $response = $httpKernel->forward('HelloBundle:Hello:fancy', array(
		 		'name' => $name,
		 		'color' => 'green',
		 		'page_title' => 'nothing to do'
		 )); */
		
		// ========== render a php template instead ==============
		/* $content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $first_name. ' '. $last_name));		
		return new Response($content); */
		
		/* $templating = $this->get('templating'); // get 'templating' service
		$content = $templating->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $first_name. ' '.$last_name));
		return new Response($content); */
		
		//return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $first_name. ' '. $last_name));
							
		// ========== Response objects ============
		$response = new Response(json_encode(array('name' => $first_name. ' ' .$last_name)));
		$response->headers->set('Content-Type', 'application/json');
    	return $response;
	}
	
	public function updateAction($request)
	{
		/*$form = $this->createForm();
		$form->bindRequest();*/
		//$this->redirect($this->generateUrl('homepage'));
		//$this->redirect($this->generateUrl('homepage'), 301);
		//return new RedirectResponse($this->generateUrl('homepage'));
			
		//$product = # get object from database
		if(!$product)
		{
			throw $this->createNotFoundException('The product does not exist.');
		}	
		
		// access sessions
		$session = $this->getRequest()->getSession();
		$session->set('foo', 'bar');
		$session->get('foo');
		$session->setLocale('fr');
		
		// set flash message
		$this->get('session')->setFlash('notice', 'Your changes were saved!');
		
		//return $this->render();
	}
	
	public function fancyAction($name, $color)
	{
		// create and return Response object
		// ============== style 1 =================
		/*$content = $this->renderView('HelloBundle:Hello:index.html.twig', array('name' => $name));
		return new Response($content);*/
		// ============== style 2 =================
		//return $this->render('HelloBundle:Hello:index.html.twig', array('name' => $name));		
		// ============== style 3 =================
		/*$templating = $this->get('templating');
		$content = $templating->render('HelloBundle:Hello:index.html.twig', array('name' => $name));*/
		// ============== access some services =========
		/*$request = $this->getRequest();
		$templating = $this->get('templating');
		$route = $this->get('router');
		$mailer = $this->get('mailer');*/
		//============ Response ===============
		/*$response = new Response('Hello'. $name, 200);*/
		$response = new Response(json_encode(array('name' => $name)));
		$response->headers->set('Content-Type', 'application/json');
		// ========== Request ==========
		$request = $this->getRequest();
		$request->isXmlHttpRequest();
		$request->getPreferredLanguage(array('en', 'fr'));
		$request->query->get('page'); //$_GET
		$request->request->get('page'); //$_POST
		
	}
}
 